import json

import scrapy


class FanyipostSpider(scrapy.Spider):
    name = "fanyiPost"
    allowed_domains = ["fanyi.baidu.com"]

    # 现在要用post方法，start_urls和parse()都为get方法，所以不用注释掉
    # start_urls = ["https://fanyi.baidu.com"]
    #
    # def parse(self, response):
    #     pass



    # 使用post方法，start_requests()为系统已经定义好的
    def start_requests(self):
        url = 'https://fanyi.baidu.com/sug'
        data = {
            'kw': '美女'
        }
        headers = {
            'Cookie': 'BIDUPSID=6F7709A126BA3DE53A77104C3D401C48; PSTM=1656320516; BAIDUID=92AB31E6FB9DB4C5AA494D7DA30757A9:FG=1; BAIDUID_BFESS=92AB31E6FB9DB4C5AA494D7DA30757A9:FG=1; BA_HECTOR=0l8ga1202kaka184ak8k042p1i10df81n; ZFY=e3x:B6oLkPbOQEY9pJb4ag9MxKb5iIXd0Ew0Rr59BBMI:C; PSINO=7; delPer=0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; ariaDefaultTheme=undefined; RT="z=1&dm=baidu.com&si=f4333bc9-a77b-4419-aeab-5c27d3bf5fb7&ss=lf80q3c2&sl=2&tt=2jx&bcn=https://fclog.baidu.com/log/weirwood?type=perf&ld=5v8&ul=6s2k&hd=6s36"; H_PS_PSSID=26350; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BCLID=10970802000874100155; BCLID_BFESS=10970802000874100155; BDSFRCVID=V8DOJexroG07VWbfL0PwulcQzLweG7bTDYrEOwXPsp3LGJLVFe3JEG0Pts1-dEu-S2OOogKK3gOTHxDF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=V8DOJexroG07VWbfL0PwulcQzLweG7bTDYrEOwXPsp3LGJLVFe3JEG0Pts1-dEu-S2OOogKK3gOTHxDF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRAOoC_-tDvDqTrP-trf5DCShUFsLjvRB2Q-XPoO3KtbSx3Pbxk20b-0hMbIhf7f5mkf3fbgy4op8P3y0bb2DUA1y4vp0tLeWeTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9QgbOKpQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHj-KD6ob3f; H_BDCLCKID_SF_BFESS=tRAOoC_-tDvDqTrP-trf5DCShUFsLjvRB2Q-XPoO3KtbSx3Pbxk20b-0hMbIhf7f5mkf3fbgy4op8P3y0bb2DUA1y4vp0tLeWeTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9QgbOKpQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0HPonHj-KD6ob3f; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1678804524; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1678808876; ab_sr=1.0.1_YTFmMjMyZDUzOWMzZGQ3ODY2ZjQ4NGNlYTY1ZTllOTg0NWQ2OGQ4ZjAwOTU0YjVkMDQzMWZlZTU1OGM5N2ZlODhjYzIyZGZjMmU3YTYyZGRkNDEyYjFmODYyYzU0NjgxZDNlNjk4NTNkZjU1OTFiNGJkZjc5MzJmYTI4NjliY2MwZWUwYTllYzE2NzU2MDMyMGU4MTdjZTYwMzkwNzkzZg=='
        }
        # 调用发起post请求的函数,callback回调函数，将请求返回的数据放在这个回调函数内
        yield scrapy.FormRequest(url=url,formdata=data,headers=headers,callback=self.parse_second)

    # 请求响应的数据在response中
    def parse_second(self,response):
        content = response.text
        cont_obj = json.loads(content)
        print(cont_obj)



